package servlet;

import Data.data;
import hbase.HbaseUtilD;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/delete")
public class Deletes extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        dodelete(request,response);
    }

    private void dodelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        HttpSession session=request.getSession();


//        HbaseUtilD hbaseUtilD = new HbaseUtilD();
        String id=request.getParameter("id");

//        ArrayList<data> dataList = new ArrayList<>();
        //  创建HBase配置对象
        Configuration configuration = HBaseConfiguration.create();
        // 指定zookeeper集群地址
        configuration.set("hbase.zookeeper.quorum", "192.168.116.11");
        // 创建连接对象
        Connection connection = ConnectionFactory.createConnection(configuration);
        // 获取Table对象,Table负责与记录相关的操作,如增删改查等
        Table table = connection.getTable(TableName.valueOf("goodsmessage"));

        Scan scan = new Scan();
        scan.setBatch(100);
        ResultScanner result = table.getScanner(scan);

        for (Result rs : result) {
            String a = null;
            String b = null;
            String c = null;
            String d = null;
            String e = null;
            String f=null;
            for (KeyValue kv : rs.raw()) {

                if (new String(kv.getQualifier()).equals("id")) {
                    a = new String(kv.getValue());
                } else if (new String(kv.getQualifier()).equals("name")) {
                    b = new String(kv.getValue());
                } else if (new String(kv.getQualifier()).equals("price")) {
                    c = new String(kv.getValue());
                } else if (new String(kv.getQualifier()).equals("time")) {
                    d = new String(kv.getValue());
                } else if (new String(kv.getQualifier()).equals("status")) {
                    e = new String(kv.getValue());
                }

                if (id.equals(a))
                {
                    f=new String(kv.getRow());
                    HbaseUtilD hbaseUtilD = new HbaseUtilD();
                    String delete=hbaseUtilD.Delete(f);
                    request.getRequestDispatcher("/freshdata").forward(request,response);
                }
            }
        }




//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter pw =  response.getWriter();
//        pw.write(delete);
    }



}
